var WxParse = require('../../wxParse/wxParse.js');

var app = getApp()
Page({
  data: {
    userInfo: {}
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.id){
      that.setData({
        id:e.id
      })
    }
    if (e && e.from) {
      that.setData({
        from: e.from
      })
    }
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          that.requestDetail();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      that.requestDetail();
    }
  },
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = e.scrollTop;
    if (scrollTop > 96 && that.data.showTitlebar != 1) {
      that.setData({ showTitlebar: 1 })
      console.log('显示'+that.data.showTitlebar)
    } else if (scrollTop <= 96 && that.data.showTitlebar != 0) {
      that.setData({ showTitlebar: 0 })
      console.log('隐藏' + that.data.showTitlebar);
      console.log(that.data.showTitlebar == 1);
    }
  },
  requestDetail: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_sequence_menu_detail',
        id: that.data.id
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data)
          var detailInfo = res.data.data;
          that.setData({
            detailInfo: detailInfo
          })
          var article = detailInfo.text;
          // 富文本解析
          WxParse.wxParse('article', 'html', article, that, 5);
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
          that.setData({
            articleInfo: ""
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestIslike: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_sequence_menu_like',
        id: that.data.id
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data)
          var detailInfo = that.data.detailInfo;
          if (detailInfo.isLike == 0) {
            if (detailInfo.likeNum < 9999) {
              detailInfo.likeNum++;
            }
            detailInfo.isLike = 1;
          } else {
            if (detailInfo.likeNum < 9999) {
              detailInfo.likeNum--;
            }
            detailInfo.isLike = 0;
          }
          that.setData({
            detailInfo: detailInfo
          })
          console.log(that.data.detailInfo);
          wx.showToast({
            title: res.data.data.msg,
            icon: 'none',
            duration: 2000
          })
        } else {
          console.log(res.data)
          wx.showToast({
            title: res.data.em,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: function () {
      }
    });
  },
  //添加到购物车
  addcart: function (e) {
    var that = this;
    var detailInfo = that.data.detailInfo;
    var goodinfo = e.currentTarget.dataset.good;
    if (goodinfo.hasFormat){
      wx.showToast({
        title: '规格商品请到详情加入购物车',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var data = {};
    data.map = 'applet_add_cart';
    data.gid = goodinfo.id;//商品id
    data.num = 1;
    data.add = 1;
    data.checkStock = 1;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          // app.errorTip(that, '加入购物车成功', 2000);
          wx.showToast({
            title: res.data.data.msg,
            icon: 'none',
            duration: 2000
          })
          detailInfo.cartTotal = res.data.data.cartNum;
          that.setData({
            isShowModal: false,
            detailInfo: detailInfo
          });
        } else {
          wx.showToast({
            title: res.data.em,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    })
  },
  toggleGoodintro: function () {
    var that = this;
    that.setData({
      isGoodintro: !that.data.isGoodintro
    })
    console.log(that.data.isGoodintro);
  },
  toggleGoodshow:function(){
    var that = this;
    that.setData({
      isGoodshow: !that.data.isGoodshow
    })
  },
  togoodDetail: function (e) {
    var goodId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages1/goodDetail/goodDetail?from=menu&id=' + goodId
    })
  },
  toback:function(){
    wx.navigateBack({
      delta:1
    })
  },
  toCartpage: function (e) {
    wx.navigateTo({
      url: '/subpages/mycart/mycart'
    })
  },
  getPoint: function (menuid) {
    var that = this;
    setTimeout(function () {
      wx.request({
        url: app.globalData.requestUrl,
        data: {
          map: 'applet_share_get_point',
          type: 'menu',
          id: menuid
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.ec == 200) {
            if (res.data.data.msg) {
              console.log(res.data.data.msg);
              var detailInfo = that.data.detailInfo;
              detailInfo.shareNum++;
              that.setData({
                detailInfo: detailInfo
              })
              // wx.showModal({
              //   title: '',
              //   content: res.data.data.msg,
              //   showCancel: false
              // })
            }
          } else {
            if (res.data.em) {
              // wx.showModal({
              //   title: '',
              //   content: res.data.em,
              //   showCancel: false
              // })
            }
          }
        },
        complete: function () {
        }
      });
    }, 3000)
  },
  onShareAppMessage: function () {
    var that = this;
    var id = that.data.id;
    var mid = app.globalData.userInfo ? app.globalData.userInfo.mid : '';
    var title = that.data.detailInfo && that.data.detailInfo.title ? that.data.detailInfo.title : '';
    var cover = that.data.detailInfo && that.data.detailInfo.cover ? that.data.detailInfo.cover : '';
    that.getPoint(id);
    return {
      title: title,
      imageUrl: cover,
      path: 'subpages0/finefoodDetail/finefoodDetail?from=share&id=' + id
    }
  }
})